package com.lhz.project.wage.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/5/1 16:08
 * @Description:
 **/
public class CustomDoubleConverter implements Converter<Double> {
    @Override
    public Class supportJavaTypeKey() {
        return Double.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    /**
     * 这里读的时候会调用
     *
     * @param cellData            NotNull
     * @param contentProperty     Nullable
     * @param globalConfiguration NotNull
     * @return
     */
    @Override
    public Double convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        CellDataTypeEnum type = cellData.getType();
        double newValue;
        if (type == CellDataTypeEnum.STRING) {
            //如果为文本则转换为为Double
            newValue = Double.parseDouble(cellData.getStringValue());
        } else {
            newValue = Double.parseDouble(cellData.getNumberValue().toString());
        }
        return newValue;
    }

    @Override
    public WriteCellData convertToExcelData(Double aDouble, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        return new WriteCellData("" + aDouble);
    }

}